import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;

public class AePlayWave extends Thread {

    private String fileName;

    public AePlayWave(String fileName) {
        this.fileName = fileName;
    }


    public void run() {


        File soundFile = new File(fileName);
        AudioInputStream audioInputStream = null;
        try {
            audioInputStream = AudioSystem.getAudioInputStream(soundFile);
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        AudioFormat format = audioInputStream.getFormat();
        SourceDataLine sourceDataLine = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
        try {
            sourceDataLine = (SourceDataLine) AudioSystem.getLine(info);
            sourceDataLine.open(format);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            return;
        }
        sourceDataLine.start();
        int byteRead = 0;
        byte[] bytes = new byte[512];
        try {
            while (byteRead != -1) {

                byteRead = audioInputStream.read(bytes, 0, bytes.length);

                if(byteRead>=0){
                    sourceDataLine.write(bytes,0,byteRead);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            sourceDataLine.drain();
            sourceDataLine.close();
        }

    }
}
